package com.evan.webadmin.biz.controller;

import com.evan.common.core.annotation.NoAuthInterface;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

@RestController
@Slf4j
public class VideoController {

    @NoAuthInterface
    @GetMapping("/admin/biz/video/welcome")
    public void getWelcomeVideo(HttpServletResponse response) throws IOException {
        try {
            response.setContentType("video/mp4");
            response.setHeader("Content-Disposition", "inline; filename=welcome.mp4");
            ResourceLoader resourceLoader = new DefaultResourceLoader();
            Resource resource = resourceLoader.getResource("classpath:/welcome.mp4");

            try (InputStream videoFileStream = resource.getInputStream()) {
                byte[] buffer = new byte[1024];
                int bytesRead;
                while ((bytesRead = videoFileStream.read(buffer)) != -1) {
                    response.getOutputStream().write(buffer, 0, bytesRead);
                }
                response.flushBuffer();
            }
        } catch (IOException e) {
           log.error("Error occurred while streaming VideoController welcome：", e);
        }
    }
}
